﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using DevExpress.LookAndFeel;
using DevExpress.Skins;
using DevExpress.Utils.Drawing;
using DevExpress.XtraEditors.Controls;

namespace XtraTaskDialogDemo
{
    public partial class XtraTaskDialogTestForm : DevExpress.XtraEditors.XtraForm
    {
        private int ProgressBarPosition = 0;
        private XtraTaskDialogFlags flags;

        public XtraTaskDialogTestForm()
        {
            InitializeComponent();

            rdgIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Application, "Application"));
            rdgIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Asterisk, "Asterisk"));
            rdgIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Error, "Error"));
            rdgIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Exclamation, "Exclamation"));
            rdgIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Hand, "Hand"));
            rdgIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Information, "Information"));
            rdgIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Question, "Question"));
            rdgIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Shield, "Shield"));
            rdgIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Warning, "Warning"));
            rdgIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.WinLogo, "WinLogo"));
            rdgIcon.EditValue = SystemIcons.Information;

            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(null, "None"));
            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Application, "Application"));
            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Asterisk, "Asterisk"));
            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Error, "Error"));
            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Exclamation, "Exclamation"));
            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Hand, "Hand"));
            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Information, "Information"));
            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Question, "Question"));
            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Shield, "Shield"));
            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.Warning, "Warning"));
            rdgFooterIcon.Properties.Items.Add(new RadioGroupItem(SystemIcons.WinLogo, "WinLogo"));
            rdgFooterIcon.EditValue = null;

            foreach (Enum value in Enum.GetValues(typeof(DialogResult)))
            {
                DialogResult tmpValue = (DialogResult)value;
                chklbButtons.Items.Add(value, value.ToString(), 
                    (tmpValue == DialogResult.Yes || tmpValue == DialogResult.No ? 
                    CheckState.Checked : CheckState.Unchecked), true);
            }

            foreach (Enum value in Enum.GetValues(typeof(XtraTaskDialogFlags)))
            {
                chklbFlags.Items.Add(value, value.ToString(), CheckState.Unchecked, true);
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            ProgressBarPosition = 0;
            flags = XtraTaskDialogFlags.None;

            bool VerificationChecked;
            int SelectedCustomButton;
            int SelectedRadioButton;

            List<DialogResult> buttons = new List<DialogResult>();
            foreach (CheckedListBoxItem item in chklbButtons.CheckedItems)
            {
                if (!(item.Value is DialogResult) || ((DialogResult)item.Value) == System.Windows.Forms.DialogResult.None)
                {
                    continue;
                }

                buttons.Add((DialogResult)item.Value);
            }

            flags = XtraTaskDialogFlags.None;
            foreach (CheckedListBoxItem item in chklbFlags.CheckedItems)
            {
                flags |= ((XtraTaskDialogFlags)item.Value);
            }

            List<XtraTaskDialogButton> radiobuttons = GetCustomButtons(txtRadioButtons.Lines);
            List<XtraTaskDialogButton> custombuttons = GetCustomButtons(txtCustomButtons.Lines);

            DialogResult result = DevExpress.XtraEditors.XtraTaskDialog.Show(new XtraTaskDialogArgs()
            {
                Buttons = buttons.ToArray(),
                Content = txtContent.Text,
                DefaultButtonIndex = (int)seDefaultButtonIndex.Value,
                ExpandedInfo = txtExpandedInfo.Text,
                Icon = ((Icon)rdgIcon.EditValue).ToBitmap(),
                MainInstruction = txtMainInstruction.Text,
                Owner = this,
                Title = txtTitle.Text,
                VerificationText = txtVerification.Text,
                FooterText = txtFooter.Text, 
                FooterIcon = (rdgFooterIcon.EditValue == null ? null : ResizeBitmap(((Icon)rdgFooterIcon.EditValue).ToBitmap(), 16, 16)),
                RadioButtons = radiobuttons.ToArray(),
                DefaultRadioButton = (int)seDefaultRadioButton.Value,
                CustomButtons = custombuttons.ToArray(),
                UseCommandLinks = chkCommandLinks.Checked,
                Flags = flags,
                NotificationDelegate = new XtraTaskDialogNotificationDelegate(TaskDialogCallback),
                DialogTimeout = (int)seDialogTimeout.Value,
                CustomProgressBarText = txtProgressBar.Text
            }, 
            out SelectedCustomButton,
            out SelectedRadioButton,
            out VerificationChecked);

            //Show the result
            MessageBox.Show(string.Format("The result of the dialog is: '{0}', the selected custom button is: '{1}', the selected ratio button is: '{2}', the verification checked value is: '{3}'",
                result, SelectedCustomButton, SelectedRadioButton, VerificationChecked));
        }

        private List<XtraTaskDialogButton> GetCustomButtons(string[] Lines)
        {
            List<XtraTaskDialogButton> radiobuttons = new List<XtraTaskDialogButton>();
            foreach (string line in Lines)
            {
                string[] parts = line.Split('|');
                if (parts.Length != 2)
                {
                    continue;
                }

                DialogResult tmpResult;
                if (Enum.TryParse<DialogResult>(parts[0], out tmpResult))
                {
                    radiobuttons.Add(new XtraTaskDialogButton(tmpResult, parts[1]));
                }
            }

            return radiobuttons;
        }

        private void TaskDialogCallback(XtraTaskDialogForm sender, XtraTaskDialogNotificationType type, object Data)
        {
            if (type == XtraTaskDialogNotificationType.Timer && flags.HasFlag(XtraTaskDialogFlags.ShowProgressBar))
            {
                if (ProgressBarPosition < seProgressBarMax.Value)
                {
                    ProgressBarPosition += 1;
                    sender.SetProgressBarPosition(ProgressBarPosition);
                }
                else
                {
                    //this.BeginInvoke(new MethodInvoker(delegate()
                    //{
                    //    sender.Close();
                    //}));
                }
            }
            else if (type == XtraTaskDialogNotificationType.Created)
            {
                //sender.SetProgressBarState(true);
                sender.SetProgressBarRange(0, (int)seProgressBarMax.Value);
            }
            else if (type == XtraTaskDialogNotificationType.HyperlinkClicked)
            {
                MessageBox.Show(string.Format("A hyperlink was clicked: '{0}'", Data));
            }
        }

        Image ResizeBitmap(Image SrcImg, int NewWidth, int NewHeight)
        {
            float percent_width = (NewWidth / (float)SrcImg.Width);
            float percent_height = (NewHeight / (float)SrcImg.Height);

            float resize_percent = (percent_height < percent_width ? percent_height : percent_width);

            int w = (int)(SrcImg.Width * resize_percent);
            int h = (int)(SrcImg.Height * resize_percent);
            Bitmap b = new Bitmap(w, h);

            using (Graphics g = Graphics.FromImage(b))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(SrcImg, 0, 0, w, h);
            }
            return b;
        }
    }
}